<template>
  <div class="categories-contain">
    <!-- 面包屑区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-button type="primary" @click="showAddCate">添加分类</el-button>
      </el-row>
      <!-- 自定义表格区域 -->
      <zk-table
        :data="cateList"
        :columns="columns"
        show-index
        index-text="#"
        :expand-type="false"
        :selection-type="false"
        :show-row-hover="false"
        border
      >
        <template slot="isDelete" slot-scope="scope">
          <i
            class="el-icon-success"
            style="color: #90ee90"
            v-if="scope.row.cat_deleted === false"
          ></i>
          <i class="el-icon-error" style="color: #f56c6c" v-else></i>
        </template>
        <template slot="sort" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag type="success" v-else-if="scope.row.cat_level === 1"
            >二级
          </el-tag>
          <el-tag type="warning" v-else>三级</el-tag>
        </template>
        <template slot="operation" slot-scope="">
          <el-button type="primary" icon="el-icon-edit" size="mini"
            >编辑</el-button
          >
          <el-button type="danger" icon="el-icon-delete" size="mini"
            >删除</el-button
          >
        </template>
      </zk-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[3, 5, 10, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 添加分类对话框 -->
    <el-dialog
      title="添加分类"
      :visible.sync="dialogVisibleAddCate"
      width="50%"
      @close="resetForm"
    >
      <el-form
        :model="cateFormData"
        :rules="rulesAddCate"
        ref="ruleFormAddCate"
        label-width="80px"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="cateFormData.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类" prop="cat_pid">
          <el-cascader
            :options="parentCateList"
            :props="cascaderProps"
            expandTrigger="hover"
            @change="parentCateChange"
            v-model="selectedId"
            clearable
          ></el-cascader>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisibleAddCate = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  created() {
    this.getCateList()
  },
  data() {
    return {
      // 分类表格数据
      cateList: [],
      // 商品分类的请求参数
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      // 分类商品的总数目
      total: 0,
      // 在columns中添加列
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          prop: 'cat_deleted',
          type: 'template',
          template: 'isDelete'
        },
        {
          label: '排序',
          prop: 'cat_level',
          type: 'template',
          template: 'sort'
        },
        {
          label: '操作',
          type: 'template',
          template: 'operation'
        }
      ],
      // 添加分类对话框是否显示
      dialogVisibleAddCate: false,
      // 添加分类的请求参数
      cateFormData: {
        cat_pid: 0,
        cat_name: '',
        cat_level: 0
      },
      // 添加表单的验证
      rulesAddCate: {
        cat_name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ]
      },
      // 配置级联菜单中数据如何展示
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        expandTrigger: 'hover',
        checkStrictly: true
      },
      // 父级分类列表数据
      parentCateList: [],
      // 选中者的id
      selectedId: []
    }
  },
  methods: {
    // 获取商品分类数据列表数据
    async getCateList() {
      const { data: res } = await this.$http.get('categories', {
        params: this.queryInfo
      })
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('获取分类列表数据失败')
      }
      this.cateList = res.data.result
      this.total = res.data.total
    },
    // 监听每页显示的条数
    handleSizeChange(val) {
      // console.log(val)
      this.queryInfo.pagesize = val
      this.getCateList()
    },
    // 监听页码
    handleCurrentChange(val) {
      // console.log(val)
      this.queryInfo.pagenum = val
      this.getCateList()
    },
    // 添加分类对话框显示
    showAddCate() {
      this.dialogVisibleAddCate = true
      this.getParentList()
    },
    // 获取父级分类列表数据
    async getParentList() {
      // 得到前两级的数据
      const { data: res } = await this.$http.get('categories', {
        params: { type: 2 }
      })
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('获取父级分类列表数据失败')
      }
      this.parentCateList = res.data
    },
    parentCateChange() {
      // console.log(this.selectedId)
      // 如果用户选择了父级分类
      if (this.selectedId.length > 0) {
        // 则将数组中的最后一项设置为父级分类
        this.cateFormData.cat_pid = this.selectedId[this.selectedId.length - 1]
        // level也要跟着发生变化
        this.cateFormData.cat_level = this.selectedId.length
      } else {
        this.cateFormData.cat_pid = 0
        this.cateFormData.cat_level = 0
      }
    },
    // 添加分类关闭时重置表单
    resetForm() {
      this.$refs.ruleFormAddCate.resetFields()
      this.selectedId = []
      this.cateFormData.cat_level = 0
    },
    // 添加分类
    addCate() {
      // console.log(this.cateFormData)
      this.$refs.ruleFormAddCate.validate(async valid => {
        if (!valid) {
          return this.$message.error('添加分类失败')
        }
        const { data: res } = await this.$http.post(
          'categories',
          this.cateFormData
        )
        if (res.meta.status !== 201) {
          return this.$message.error('添加分类失败')
        }
        this.$message.success('添加分类成功')
        this.dialogVisibleAddCate = false
        this.getCateList()
      })
    }
  }
}
</script>

<style lang="less" scoped>
.zk-table {
  margin-bottom: 15px;
}
</style>
